package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentActivity extends AppCompatActivity {

    private TextView commentsView; // 标题
    private TextView commentContent; // 显示评论内容
    private EditText commentInput;
    private Button submitComment;
    private String movieName;

    public static Map<String, List<String>> movieComments = new HashMap<>(); // 存储每部电影的评论

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);

        commentsView = findViewById(R.id.commentsView); // 获取 commentsView
        commentContent = findViewById(R.id.commentContent); // 获取 commentContent
        commentInput = findViewById(R.id.commentInput);
        submitComment = findViewById(R.id.submitComment);

        // 获取传递的电影名称
        movieName = getIntent().getStringExtra("movieName");

        // 初始化评论列表
        List<String> comments = movieComments.getOrDefault(movieName, new ArrayList<>());
        updateCommentsContent(comments); // 使用这个方法更新 commentsContent

        // 设置提交评论按钮的点击事件
        submitComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String commentText = commentInput.getText().toString();
                if (!commentText.isEmpty()) {
                    // 添加评论到列表
                    List<String> comments = movieComments.getOrDefault(movieName, new ArrayList<>());
                    comments.add(commentText);
                    movieComments.put(movieName, comments);

                    // 更新评论列表显示
                    updateCommentsContent(comments); // 使用这个方法更新 commentsContent

                    // 清空输入框
                    commentInput.setText("");

                    // 显示提示信息
                    Toast.makeText(CommentActivity.this, "评论已提交", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(CommentActivity.this, "请输入评论内容", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 更新 commentsContent 的内容
    private void updateCommentsContent(List<String> comments) {
        if (comments.isEmpty()) {
            commentContent.setText("暂无评论"); // 如果没有评论，显示 "暂无评论"
        } else {
            // 将评论列表显示在 TextView 上
            StringBuilder sb = new StringBuilder();
            for (String comment : comments) {
                sb.append(comment).append("\n");
            }
            commentContent.setText(sb.toString());
        }
    }
}